package com.fjl.filewriter;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class HelloFileTest {
	public static void main(String[] args) {

//		 writeToTxt("test4\r\n");
		 write2file("test", new File("D:/fjl.txt"));

		// String readStr = readFile(new File("D:/fjl.txt"));
		// System.out.println(readStr);

		// String readStr = readFile("D:/fjl.txt");
		// System.out.println(readStr);
		
//		getCreateTime("D:/fjl.txt");
//		getModifiedTime_1("D:/fjl.txt");
//		getModifiedTime_2("D:/fjl.txt");

	}

	/**
	 * 读取文件
	 * 
	 * @param filePath
	 * @return String
	 */
	public static String readFile(String filePath) {
		try {
			byte[] buff = new byte[256];
			FileInputStream fis = new FileInputStream(filePath);
			int len = fis.read(buff);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			outputStream.write(buff, 0, len);
			return new String(outputStream.toByteArray());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 读文件的得到文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] readFileToBytes(String filePath) {
		byte[] bytes = null;
		try {
			BufferedInputStream bi = new BufferedInputStream(
					new FileInputStream(filePath));
			// 输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bytes;

	}

	// 追加到文件尾
	public static void write2file(String string, File file) {
		String context = string + "\r\n";
		try {
			FileOutputStream fOutputStream = new FileOutputStream(file, true);// 设置为true，可以追加
			fOutputStream.write(context.getBytes());
			fOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeToTxt(String str) {

		try {
			// BufferedWriter bfw = new BufferedWriter(new
			// FileWriter("D:/text.txt"));
			// bfw.append("hello java");
			// bfw.flush();
			// bfw.close();
			// System.out.println("hello");

			FileWriter fw = new FileWriter(new File("D:/fjl.txt"), true);
			fw.append(str);
			fw.flush();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	 /** 
     * 读取文件创建时间 
     */  
    public static void getCreateTime(String filePath){  
        String strTime = null;  
        try {  
            Process p = Runtime.getRuntime().exec("cmd /C dir "           
                    + filePath  
                    + "/tc" );  
            InputStream is = p.getInputStream();   
            BufferedReader br = new BufferedReader(new InputStreamReader(is));             
            String line;  
            while((line = br.readLine()) != null){  
                if(line.endsWith(".txt")){  
                    strTime = line.substring(0,17);  
                    break;  
                }                             
             }   
        } catch (IOException e) {  
            e.printStackTrace();  
        }         
        System.out.println("创建时间    " + strTime);     
        //输出：创建时间   2009-08-17  10:21  
    }  
    /** 
     * 读取文件修改时间的方法1 
     */   
    @SuppressWarnings("deprecation")  
    public static void getModifiedTime_1(String filePath){  
        File f = new File(filePath);              
        Calendar cal = Calendar.getInstance();  
        long time = f.lastModified();  
        cal.setTimeInMillis(time);    
        //此处toLocalString()方法是不推荐的，但是仍可输出  
        System.out.println("修改时间[1] " + cal.getTime().toLocaleString());   
    }  
      
    /** 
     * 读取修改时间的方法2 
     */  
    public static void getModifiedTime_2(String filePath){  
        File f = new File(filePath);              
        Calendar cal = Calendar.getInstance();  
        long time = f.lastModified();  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         
        cal.setTimeInMillis(time);    
        System.out.println("修改时间[2] " + formatter.format(cal.getTime()));     
    }  
}
